//UPDATED: 11/22/2010
package com.cs589.smscrypto;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;
/*
 * This activity is centered on decrypting messages that the 
 * user has received. To make our job easier, the user is required
 * to enter the encrypted message. The decrypted message will 
 * then be displayed to the user.
 * Reference:
 * http://code.google.com/p/krvarma-android-samples/source/browse/trunk/SMSDemo/src/com/varma/samples/smsdemo/MainActivity.java
 */
public class DecryptSMS extends Activity {
    /** Called when the activity is first created. */
	TextView messageRxtv;
	TextView contactRxtv;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.decryptsms);
        messageRxtv = (TextView)this.findViewById(R.id.messageRx);
        contactRxtv = (TextView)this.findViewById(R.id.sender);
        
        Encrypt test = new Encrypt();
        
        test.getPrivate("5056880936", "42");
        
        registerReceiver(smsreceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
    }

    @Override
    protected void onDestroy()
    {
	    unregisterReceiver(smsreceiver);
	    super.onDestroy();
    }
    
    //Receive SMS message
    private BroadcastReceiver smsreceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
	        Bundle bundle = intent.getExtras();        
	        SmsMessage[] msgs = null;
        
	        if(null != bundle)
	        {
	            String info = "Text SMS from ";
	            Object[] pdus = (Object[]) bundle.get("pdus");
	            msgs = new SmsMessage[pdus.length];
	            
	            for (int i=0; i<msgs.length; i++){
	                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
	                String contact = msgs[i].getOriginatingAddress();
	                contactRxtv.setText(contact);
	                
	                info += msgs[i].getMessageBody().toString();
	                messageRxtv.setText(info);
	            }
	            Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
	        }                         
        }
    };
}